****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																		  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		MERGING AND RECODING FINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Appending datasets
use "CCES_2008_recoded2.dta", clear
append using "CCES_2006_recoded2.dta"
append using "CCES_2010_recoded2.dta"
append using "CCES_2012_recoded2.dta"
append using "CCES_2014_recoded2.dta"
append using "CCES_2016_recoded2.dta"
append using "CCES_2018_recoded2.dta"
sort year

*partisanship - profile
fre pid7 // all categories
recode pid7 8=., gen(pid7prof) // 7 categories, including leaners
rename pid3 pid3prof_orig // origina var, leaners coded as independent/other/dk
label var pid3prof_orig "3 cat. PID original var - leaners coded as indep/other/dk"
fre pid3prof_orig
ta pid7 pid3prof_orig
ta year pid3prof_orig
fre pid7
recode pid7 4=. 8=., gen(pid6prof)
recode pid7 1/3=1 4=3 5/7=2 8=., gen(pid3prof)
recode pid7 1/3=1 4=. 5/7=2 8=., gen(pid2prof)
recode pid7 3=1 5=1 else=0, gen(leaners_prof)
recode pid3prof (3=0 "independent") (1/2=1 "partisans"), gen(partisans)

*partisanship - post
rename pid3post pid3post_orig
label var pid3post_orig "3 cat. PID POST - leaners coded as indep/other/dk"
fre pid3post_orig
*pid5 1"Democrat" 2"Lean Dem." 3"Independent" 4"Lean Rep." 5"Republican"
fre pid5post
recode pid5post 1/2=1 3=3 4/5=2, gen(pid3post)
recode pid5post 1/2=1 3=. 4/5=2, gen(pid2post)
recode pid5post 2=1 4=1 else=0, gen(leaners_post)

*ideology - profile
fre ideo5
rename ideo5 ideo5_full
recode ideo5_full 6=., gen(ideo5prof)
recode ideo5prof (1/2=1) (3=3) (4/5=2), gen(ideo3prof)
recode ideo5prof (1/2=1) (3=.) (4/5=2), gen(ideo2prof)
recode ideo5prof (3=0) (2 4=1) (1 5=2), gen(ideostr_prof)

*ideology - pre-elections
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con, 8dk
fre ideo7
ta year if ideo7!=.
rename ideo7 ideo7pre_full
fre ideo7pre_full
recode ideo7pre_full 8=., gen(ideo7pre)
recode ideo7pre 4=., gen(ideo6pre)
recode ideo7pre 1/3=1 4=3 5/7=2, gen(ideo3pre)
recode ideo7pre 1/3=1 4=. 5/7=2, gen(ideo2pre)
recode ideo7pre (4=0) (3 5=1) (2 6=2) (1 7=3), gen(ideostr_pre)
recode ideo3pre (3=0 "moderates") (1/2=1 "lib/con"), gen(ideology)

*economy
fre economy
recode economy 6=., gen(economyr)
label val economyr CC302
recode economy (1 2=1 "got better") (3=2 "stayed the same") (4 5=3 "got worse") (6=.), gen(economy3)
fre economy3

*new filter
clonevar filter_sen1 = filter_sen
replace filter_sen1 = 2 if state==32 & year==2012 // nevada in 2012 technically didn't have an incumbent
tab1 filter_sen filter_sen1

*seats with dem+rep can, by incumbent status & filter
*senate
fre inc_rep_sen
fre inc_rep_sen if filter_sen1==1
fre inc_all_sen
fre inc_all_sen if filter_sen1==1
gen inc_all_sen_filter = inc_all_sen if filter_sen1==1
replace inc_all_sen_filter = 0 if inc_all_sen==0
fre inc_all_sen_filter
*house
ta inc_all_hou
ta inc_all_hou if filter_hou==1
gen inc_all_hou_filter = inc_all_hou if filter_hou==1
replace inc_all_hou_filter = 0 if inc_all_hou==0
fre inc_all_hou_filter

*apply labels
do "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\02_labels_merged_dataset.do"

******
*recode voting var

*senate
fre vote_sen
fre SenCand1Party_postr SenCand2Party_postr
gen votedemrep_sen = 0 if (vote_sen==1 & SenCand1Party_postr==1) | (vote_sen==2 & SenCand2Party_postr==1)
replace votedemrep_sen = 1 if vote_sen==2 & SenCand2Party_postr==2
label var votedemrep_sen "Voted for Republican vs democrat candidate Senate"
label de votedemrep_sen 1"republican" 0 "democratic candidate"
label val votedemrep_sen votedemrep_sen
fre votedemrep_sen
*house
fre vote_hou
fre HouseCand1Party_postr HouseCand2Party_postr
gen votedemrep_hou = 0 if (vote_hou==1 & HouseCand1Party_postr==1) | (vote_hou==2 & HouseCand2Party_postr==1)
replace votedemrep_hou = 1 if (vote_hou==1 & HouseCand1Party_postr==2) | (vote_hou==2 & HouseCand2Party_postr==2)
label var votedemrep_hou "Voted for Republican vs democratic candidate House"
label val votedemrep_hou votedemrep_sen
fre votedemrep_hou


***********************************************
*SENATE
***********************************************

*create combined variables - SENATE
fre inc_rep_sen

*incumbent from own party - pid3prof
fre inc_rep_sen pid3prof
*democrats
gen own_incD = inc_rep_sen if pid3prof==1
recode own_incD 1=0 0=1 .=0 if inc_rep_sen!=.
*republicans
gen own_incR = inc_rep_sen if pid3prof==2
recode own_incR .=0 if inc_rep_sen!=.
*independents (rep inc = own inc)
gen own_incI = inc_rep_sen if pid3prof==3
recode own_incI .=0 if inc_rep_sen!=.
*combined
gen own_incDRIprof_sen = own_incD + own_incR + own_incI
label var own_incDRIprof_sen "Senate incumbent from ..."
label de own_inc 0"other party" 1"own party", modify
label val own_incDRIprof_sen own_inc
fre own_incDRIprof_sen
ta pid3prof inc_rep_sen
ta pid3prof own_incDRIprof_sen
drop own_incD own_incR own_incI

*incumbent from own party - pid3post
fre inc_rep_sen pid3post
*democrats
gen own_incD = inc_rep_sen if pid3post==1
recode own_incD 1=0 0=1 .=0 if inc_rep_sen!=.
*republicans
gen own_incR = inc_rep_sen if pid3post==2
recode own_incR .=0 if inc_rep_sen!=.
*independents (rep inc = own inc)
gen own_incI = inc_rep_sen if pid3post==3
recode own_incI .=0 if inc_rep_sen!=.
*combined
gen own_incDRIpost_sen = own_incD + own_incR + own_incI
label var own_incDRIpost_sen "Senate incumbent from ..."
label val own_incDRIpost_sen own_inc
fre own_incDRIpost_sen
ta pid3post inc_rep_sen
ta pid3post own_incDRIpost_sen
drop own_incD own_incR own_incI

*incumbent with same ideology - ideo3prof
fre inc_rep_sen ideo3prof
*liberals
gen own_incL = inc_rep_sen if ideo3prof==1
recode own_incL 1=0 0=1 .=0 if inc_rep_sen!=.
*conservatives
gen own_incC = inc_rep_sen if ideo3prof==2
recode own_incC .=0 if inc_rep_sen!=.
*moderates
gen own_incM = inc_rep_sen if ideo3prof==3
recode own_incM .=0 if inc_rep_sen!=.
*combined
gen own_incLCMprof_sen = own_incL + own_incC + own_incM
label var own_incLCMprof_sen "Senate incumbent with ..."
label de own_incLC 0"other ideology" 1"own ideology"
label val own_incLCMprof_sen own_incLC
fre own_incLCMprof_sen
ta ideo3prof inc_rep_sen
ta ideo3prof own_incLCMprof_sen
drop own_incL own_incC own_incM

*incumbent with same ideology - ideo3pre
fre inc_rep_sen ideo3pre
*liberals
gen own_incL = inc_rep_sen if ideo3pre==1
recode own_incL 1=0 0=1 .=0 if inc_rep_sen!=.
*conservatives
gen own_incC = inc_rep_sen if ideo3pre==2
recode own_incC .=0 if inc_rep_sen!=.
*moderates
gen own_incM = inc_rep_sen if ideo3pre==3
recode own_incM .=0 if inc_rep_sen!=.
*combined
gen own_incLCMpre_sen = own_incL + own_incC + own_incM
label var own_incLCMpre_sen "Senate incumbent with ..."
label val own_incLCMpre_sen own_incLC
fre own_incLCMprof_sen
ta ideo3pre inc_rep_sen
ta ideo3pre own_incLCMpre_sen
drop own_incL own_incC own_incM


*candidate from own party - pid3prof
fre votedemrep_sen pid3prof
*democrats
gen own_candD = votedemrep_sen if pid3prof==1
recode own_candD 1=0 0=1 .=0 if votedemrep_sen!=.
*republicans
gen own_candR = votedemrep_sen if pid3prof==2
recode own_candR .=0 if votedemrep_sen!=.
*independents
gen own_candI = votedemrep_sen if pid3prof==3
recode own_candI .=0 if votedemrep_sen!=.
*combined
gen own_candDRIprof_sen = own_candD + own_candR + own_candI
label var own_candDRIprof_sen "Vote for candidate from ... (SENATE)"
label de own_cand 0"other party" 1"own party", modify
label val own_candDRIprof_sen own_cand
fre own_candDRIprof_sen
ta pid3prof votedemrep_sen
ta pid3prof own_candDRIprof_sen
drop own_candD own_candR own_candI

*candidate from own party - pid3post
fre votedemrep_sen pid3post
*democrats
gen own_candD = votedemrep_sen if pid3post==1
recode own_candD 1=0 0=1 .=0 if votedemrep_sen!=.
*republicans
gen own_candR = votedemrep_sen if pid3post==2
recode own_candR .=0 if votedemrep_sen!=.
*independents
gen own_candI = votedemrep_sen if pid3post==3
recode own_candI .=0 if votedemrep_sen!=.
*combined
gen own_candDRIpost_sen = own_candD + own_candR + own_candI
label var own_candDRIpost_sen "Vote for candidate from ... (SENATE)"
label val own_candDRIpost_sen own_cand
fre own_candDRIpost_sen
ta pid3post votedemrep_sen
ta pid3post own_candDRIpost_sen
drop own_candD own_candR own_candI

*candidate with same ideology - ideo3prof
fre votedemrep_sen ideo3prof
*liberals
gen own_candL = votedemrep_sen if ideo3prof==1
recode own_candL 1=0 0=1 .=0 if votedemrep_sen!=.
*conservatives
gen own_candC = votedemrep_sen if ideo3prof==2
recode own_candC .=0 if votedemrep_sen!=.
*moderates
gen own_candM = votedemrep_sen if ideo3prof==3
recode own_candM .=0 if votedemrep_sen!=.
*combined
gen own_candLCMprof_sen = own_candL + own_candC + own_candM
label var own_candLCMprof_sen "Vote for candidate with ... (SENATE)"
label val own_candLCMprof_sen own_incLC
fre own_candLCMprof_sen
ta ideo3prof votedemrep_sen
ta ideo3prof own_candLCMprof_sen
drop own_candL own_candC own_candM

*candidate with same ideology - ideo3pre
fre votedemrep_sen ideo3pre
*liberals
gen own_candL = votedemrep_sen if ideo3pre==1
recode own_candL 1=0 0=1 .=0 if votedemrep_sen!=.
*conservatives
gen own_candC = votedemrep_sen if ideo3pre==2
recode own_candC .=0 if votedemrep_sen!=.
*moderates
gen own_candM = votedemrep_sen if ideo3pre==3
recode own_candM .=0 if votedemrep_sen!=.
*combined
gen own_candLCMpre_sen = own_candL + own_candC + own_candM
label var own_candLCMpre_sen "Vote for candidate with ... (SENATE)"
label val own_candLCMpre_sen own_incLC
fre own_candLCMpre_sen
ta ideo3pre votedemrep_sen
ta ideo3pre own_candLCMpre_sen
drop own_candL own_candC own_candM


***********************************************
*HOUSE
***********************************************

*incumbent from own party - pid3prof
fre inc_rep_hou pid3prof
*democrats
gen own_incD = inc_rep_hou if pid3prof==1
recode own_incD 1=0 0=1 .=0 if inc_rep_hou!=.
*republicans
gen own_incR = inc_rep_hou if pid3prof==2
recode own_incR .=0 if inc_rep_hou!=.
*independents (rep inc = own inc)
gen own_incI = inc_rep_hou if pid3prof==3
recode own_incI .=0 if inc_rep_hou!=.
*combined
gen own_incDRIprof_hou = own_incD + own_incR + own_incI
label var own_incDRIprof_hou "House incumbent from ..."
label val own_incDRIprof_hou own_inc
fre own_incDRIprof_hou
ta pid3prof inc_rep_hou
ta pid3prof own_incDRIprof_hou
drop own_incD own_incR own_incI

*incumbent from own party - pid3post
fre inc_rep_hou pid3post
*democrats
gen own_incD = inc_rep_hou if pid3post==1
recode own_incD 1=0 0=1 .=0 if inc_rep_hou!=.
*republicans
gen own_incR = inc_rep_hou if pid3post==2
recode own_incR .=0 if inc_rep_hou!=.
*independents (rep inc = own inc)
gen own_incI = inc_rep_hou if pid3post==3
recode own_incI .=0 if inc_rep_hou!=.
*combined
gen own_incDRIpost_hou = own_incD + own_incR + own_incI
label var own_incDRIpost_hou "House incumbent from ..."
label val own_incDRIpost_hou own_inc
fre own_incDRIpost_hou
ta pid3post inc_rep_hou
ta pid3post own_incDRIpost_hou
drop own_incD own_incR own_incI

*incumbent with same ideology - ideo3prof
fre inc_rep_hou ideo3prof
*liberals
gen own_incL = inc_rep_hou if ideo3prof==1
recode own_incL 1=0 0=1 .=0 if inc_rep_hou!=.
*conservatives
gen own_incC = inc_rep_hou if ideo3prof==2
recode own_incC .=0 if inc_rep_hou!=.
*moderates
gen own_incM = inc_rep_hou if ideo3prof==3
recode own_incM .=0 if inc_rep_hou!=.
*combined
gen own_incLCMprof_hou = own_incL + own_incC + own_incM
label var own_incLCMprof_hou "House incumbent with ..."
label val own_incLCMprof_hou own_incLC
fre own_incLCMprof_hou
ta ideo3prof inc_rep_hou
ta ideo3prof own_incLCMprof_hou
drop own_incL own_incC own_incM

*incumbent with same ideology - ideo3pre
fre inc_rep_hou ideo3pre
*liberals
gen own_incL = inc_rep_hou if ideo3pre==1
recode own_incL 1=0 0=1 .=0 if inc_rep_hou!=.
*conservatives
gen own_incC = inc_rep_hou if ideo3pre==2
recode own_incC .=0 if inc_rep_hou!=.
*moderates
gen own_incM = inc_rep_hou if ideo3pre==3
recode own_incM .=0 if inc_rep_hou!=.
*combined
gen own_incLCMpre_hou = own_incL + own_incC + own_incM
label var own_incLCMpre_hou "House incumbent with ..."
label val own_incLCMpre_hou own_incLC
fre own_incLCMprof_hou
ta ideo3pre inc_rep_hou
ta ideo3pre own_incLCMpre_hou
drop own_incL own_incC own_incM

*candidate from own party - pid3prof
fre votedemrep_hou pid3prof
*democrats
gen own_candD = votedemrep_hou if pid3prof==1
recode own_candD 1=0 0=1 .=0 if votedemrep_hou!=.
*republicans
gen own_candR = votedemrep_hou if pid3prof==2
recode own_candR .=0 if votedemrep_hou!=.
*independents
gen own_candI = votedemrep_hou if pid3prof==3
recode own_candI .=0 if votedemrep_hou!=.
*combined
gen own_candDRIprof_hou = own_candD + own_candR + own_candI
label var own_candDRIprof_hou "Vote for candidate from ... (HOUSE)"
label val own_candDRIprof_hou own_cand
fre own_candDRIprof_hou
ta pid3prof votedemrep_hou
ta pid3prof own_candDRIprof_hou
drop own_candD own_candR own_candI

*candidate from own party - pid3post
fre votedemrep_hou pid3post
*democrats
gen own_candD = votedemrep_hou if pid3post==1
recode own_candD 1=0 0=1 .=0 if votedemrep_hou!=.
*republicans
gen own_candR = votedemrep_hou if pid3post==2
recode own_candR .=0 if votedemrep_hou!=.
*independents
gen own_candI = votedemrep_hou if pid3post==3
recode own_candI .=0 if votedemrep_hou!=.
*combined
gen own_candDRIpost_hou = own_candD + own_candR + own_candI
label var own_candDRIpost_hou "Vote for candidate from ... (HOUSE)"
label val own_candDRIpost_hou own_cand
fre own_candDRIpost_hou
ta pid3post votedemrep_hou
ta pid3post own_candDRIpost_hou
drop own_candD own_candR own_candI

*candidate with same ideology - ideo3prof
fre votedemrep_hou ideo3prof
*liberals
gen own_candL = votedemrep_hou if ideo3prof==1
recode own_candL 1=0 0=1 .=0 if votedemrep_hou!=.
*conservatives
gen own_candC = votedemrep_hou if ideo3prof==2
recode own_candC .=0 if votedemrep_hou!=.
*moderates
gen own_candM = votedemrep_hou if ideo3prof==3
recode own_candM .=0 if votedemrep_hou!=.
*combined
gen own_candLCMprof_hou = own_candL + own_candC + own_candM
label var own_candLCMprof_hou "Vote for candidate with ... (HOUSE)"
label val own_candLCMprof_hou own_incLC
fre own_candLCMprof_hou
ta ideo3prof votedemrep_hou
ta ideo3prof own_candLCMprof_hou
drop own_candL own_candC own_candM

*candidate with same ideology - ideo3pre
fre votedemrep_hou ideo3pre
*liberals
gen own_candL = votedemrep_hou if ideo3pre==1
recode own_candL 1=0 0=1 .=0 if votedemrep_hou!=.
*conservatives
gen own_candC = votedemrep_hou if ideo3pre==2
recode own_candC .=0 if votedemrep_hou!=.
*moderates
gen own_candM = votedemrep_hou if ideo3pre==3
recode own_candM .=0 if votedemrep_hou!=.
*combined
gen own_candLCMpre_hou = own_candL + own_candC + own_candM
label var own_candLCMpre_hou "Vote for candidate with ... (HOUSE)"
label val own_candLCMpre_hou own_incLC
fre own_candLCMpre_hou
ta ideo3pre votedemrep_hou
ta ideo3pre own_candLCMpre_hou
drop own_candL own_candC own_candM


***************************************
*NEW INCUMBENT DATA

*check incumbent variables
fre inc_rep_hou inc_all_hou inc_all_hou_filter
fre inc_hou_new 
ta inc_hou_new inc_all_hou, row

*create new var based on new data
fre inc_hou_new po1 po2 // some missing values
fre inc_hou_new if po1==.
gen inc_rep_hou2 = 0 if inc_hou_new==1
replace inc_rep_hou2 = 1 if inc_hou_new==0
label val inc_rep_hou2 inc_rep
label var inc_rep_hou2 "Dem/Rep incumbent - House NEW DATA"

*create new var based on new data - PREVIOUS ELECTION
fre inc_hou_pre if po1_pre==.
gen inc_rep_hou_pre = 0 if inc_hou_pre==1
replace inc_rep_hou_pre = 1 if inc_hou_pre==0
label val inc_rep_hou_pre inc_rep
label var inc_rep_hou_pre "Dem/Rep incumbent - House NEW DATA PREVIOUS ELECTION"

*create new var based on new data - POST ELECTION
fre inc_hou_post if po1_post==.
gen inc_rep_hou_post = 0 if inc_hou_post==1
replace inc_rep_hou_post = 1 if inc_hou_post==0
label val inc_rep_hou_post inc_rep
label var inc_rep_hou_post "Dem/Rep incumbent - House NEW DATA POST ELECTION"

*Create filter for HOUSE
fre po1 po2
fre po1 if inc_rep_hou2!=.
fre po2 if inc_rep_hou2!=. & po1<2
fre fr if inc_rep_hou2!=. & po1<2
gen filter_hou_new = 1 if inc_rep_hou2!=. & po1<2 & fr!=9
fre po1 fr if filter_hou_new==1
fre inc_rep_hou2
fre inc_rep_hou2 if filter_hou_new==1

*including open seats
ta inc_hou_new po1
fre inc_hou_new po1
gen inc_all_hou2 = 0 if (inc_hou_new==2 | inc_hou_new==3 | inc_hou_new==4) & po1<6
replace inc_all_hou2 = 1 if inc_rep_hou2==0 & po1<2 & fr!=9
replace inc_all_hou2 = 2 if inc_rep_hou2==1 & po1<2 & fr!=9
label de inc_all 0"open seat with dem vs rep" 1"dem inc (rep not inc)" 2"rep inc (dem not inc)"
label val inc_all_hou2 inc_all
fre inc_all_hou2 // already filtered
fre inc_rep_hou2 if filter_hou_new==1

*including open seats - previous election
ta inc_hou_pre po1_pre
fre inc_hou_pre po1_pre
gen inc_all_hou_pre = 0 if (inc_hou_pre==2 | inc_hou_pre==3 | inc_hou_pre==4) & po1_pre<6
replace inc_all_hou_pre = 1 if inc_rep_hou_pre==0 & po1_pre<2
replace inc_all_hou_pre = 2 if inc_rep_hou_pre==1 & po1_pre<2
label val inc_all_hou_pre inc_all
fre inc_all_hou_pre // already filtered
fre inc_all_hou_pre if filter_hou_new==1

*including open seats - POST election
ta inc_hou_post po1_post
fre inc_hou_post po1_post
gen inc_all_hou_post = 0 if (inc_hou_post==2 | inc_hou_post==3 | inc_hou_post==4) & po1_post<6
replace inc_all_hou_post = 1 if inc_rep_hou_post==0 & po1_post<2
replace inc_all_hou_post = 2 if inc_rep_hou_post==1 & po1_post<2
label val inc_all_hou_post inc_all


*incumbent with same ideology - ideo3pre
fre inc_rep_hou2 ideo3pre
*liberals
gen own_incL = inc_rep_hou2 if ideo3pre==1
recode own_incL 1=0 0=1 .=0 if inc_rep_hou2!=.
*conservatives
gen own_incC = inc_rep_hou2 if ideo3pre==2
recode own_incC .=0 if inc_rep_hou2!=.
*moderates
gen own_incM = inc_rep_hou2 if ideo3pre==3
recode own_incM .=0 if inc_rep_hou2!=.
*combined
gen own_incLCMpre_hou2 = own_incL + own_incC + own_incM
label var own_incLCMpre_hou2 "House incumbent with ..."
label val own_incLCMpre_hou2 own_incLC
fre own_incLCMpre_hou2
ta ideo3pre inc_rep_hou2
ta ideo3pre own_incLCMpre_hou2
drop own_incL own_incC own_incM

*incumbent from own party - pid3prof
fre inc_rep_hou2 pid3prof
*democrats
gen own_incD = inc_rep_hou2 if pid3prof==1
recode own_incD 1=0 0=1 .=0 if inc_rep_hou2!=.
*republicans
gen own_incR = inc_rep_hou2 if pid3prof==2
recode own_incR .=0 if inc_rep_hou2!=.
*independents (rep inc = own inc)
gen own_incI = inc_rep_hou2 if pid3prof==3
recode own_incI .=0 if inc_rep_hou2!=.
*combined
gen own_incDRIprof_hou2 = own_incD + own_incR + own_incI
label var own_incDRIprof_hou2 "House incumbent from ..."
label val own_incDRIprof_hou2 own_inc
fre own_incDRIprof_hou2
ta pid3prof inc_rep_hou2
ta pid3prof own_incDRIprof_hou2
drop own_incD own_incR own_incI

****
*create regions/divisions

fre staten
*create division
gen staten2 = staten if staten<9
replace staten2 = staten+1 if staten>8
fre staten2
gen division = .
replace division = 1 if staten2==7 | staten2==20 | staten2==22 | staten2==30 | staten2==40 | staten2==46
replace division = 2 if staten2==31 | staten2==33 | staten2==39 
replace division = 3 if staten2==14 | staten2==15 | staten2==23 | staten2==36 | staten2==50 
replace division = 4 if staten2==16 | staten2==17 | staten2==24 | staten2==26 | staten2==28 | staten2==35 | staten2==42 
replace division = 5 if staten2==8 | staten2==9 | staten2==10 | staten2==11 | staten2==21 | staten2==34 | staten2==41 | staten2==47 | staten2==49 
replace division = 6 if staten2==1 | staten2==18 | staten2==25 | staten2==43 
replace division = 7 if staten2==4 | staten2==19 | staten2==37 | staten2==44 
replace division = 8 if staten2==3 | staten2==6 | staten2==13 | staten2==27 | staten2==29 | staten2==32 | staten2==45 | staten2==51 
replace division = 9 if staten2==2 | staten2==5 | staten2==12 | staten2==38 | staten2==48 
label de division 1"New England" 2"Mid-Atlantic" 3"East North Central" 4"West North Central" ///
5"South Atlantic" 6"East South Central" 7"West South Central" 8"Mountain" 9"Pacific"
label val division division
fre division //1569
drop staten2

*census regions
recode division (1 2=1 "northeast") (3 4=2 "midwest") (5/7=3 "south") (8 9=4 "west"), gen(region)
fre region


**********************
*Additional controls
*challenger held offices
fre po1
des po1
recode po1 2/7=., gen(held_offices1)
label val held_offices1 po1
recode po1 (0=0 "challenger has not held office") ///
(1=1 "challenger has held elective office") ///
(2/7=99 "other"), gen(held_offices2)
fre held_offices*
*freshman
fre fr
recode fr (0=0 "not freshman") (1/3=1 "freshman") (9=99 "seat not defended by major party incumbent"), gen(freshman)
fre freshman
*redistricting
fre redist
*dem/rep expenses
sum dexp rexp, d
*graph box dexp rexp // clear outliers
xtile dexp10 = dexp, nq(10)
xtile rexp10 = rexp, nq(10)
gen ldexp = log(dexp)
gen lrexp = log(rexp)
*graph box ldexp lrexp // still outliers

*competitive districts
sum dvp, d
gen partygap= (abs(50-dvp))*2
label var partygap "Vote gap between Dem & Rep party in previous election"
sum partygap, d
ta dvp if partygap<0.16
ta dvp partygap if partygap<0.20
ta dvp partygap if partygap>70
*normalized
egen min_gap  =  min(partygap)  
egen max_gap  =  max(partygap)  
gen partygap01  =  (partygap  -  min_gap)/(max_gap  -  min_gap)
sum partygap01, d // from 0 (min gap) to 1 (max gap)
drop min_gap max_gap

 
*******************
*save recoded file
save "C:\Users\Davide Morisi\Dropbox\datasets\CCES\CCES_merged_dataset9.dta", replace











